package com.showde.shoppingmall.activity;

import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

import com.showde.shoppingmall.R;
import com.showde.shoppingmall.business.logic.AddressLogic;
import com.showde.shoppingmall.business.logic.CallbackData;
import com.showde.shoppingmall.business.logic.ProductCategoryLogic;
import com.showde.shoppingmall.business.logic.UICallbackInterface;
import com.showde.shoppingmall.domain.Address;
import com.showde.shoppingmall.utils.ActivityDispatch;
import com.showde.shoppingmall.utils.FaceImageFetchUtils;
import com.showde.shoppingmall.utils.ValueConstant;

public class IndexPageActivity extends BaseActivity implements ViewFactory {

	private static final String TAG = "IndexPageActivity";

	private final static int MSG_SWITCH_P = 0x100001; // 切换图片

	/**
	 * 默认图片数组
	 */
	private static final int[] DEFAULT_IMAGE_ARRAY = new int[] { R.drawable.p1 };

	/**
	 * 图片切换器
	 */
	private ImageSwitcher imageSwitcher;

	/**
	 * 非默认图片
	 */
	private Drawable[] drawables;

	private int currentPIndex; // 当前图片下标

	private boolean needRunning = true;

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_SWITCH_P:
				startSwitchPictures();
				break;
			}
			super.handleMessage(msg);
		}

	};

	@Override
	protected void onResume() {
		super.onResume();
		ProductCategoryLogic.updateProductCategory(this,
				new UICallbackInterface() {

					@Override
					public void onSuccess(CallbackData data) {
						Log.d(TAG, "获取商品类别成功");
					}

					@Override
					public void onError(String errorMsg) {
						Log.d(TAG, "获取商品类别失败");
					}
				});

	};

	@Override
	public void onClick(View v) {

	}

	@Override
	protected void initParam() {
		// 获取新下载的图片
		drawables = FaceImageFetchUtils.loadImage(this,
				FaceImageFetchUtils.getLocalImageVersion(this));

		// Notify notify = new Notify() {
		//
		// @Override
		// public void notifyNetCallback(NetEntity netEntity) {
		// JSONObject result = netEntity.result;
		// Log.d("IndexPageActivity", "success:::" + result.toString());
		// }
		//
		// @Override
		// public void notifyError(int httpResponseCode, String errorMsg) {
		// Log.d("IndexPageActivity", "fail:::httpResponseCode::"
		// + httpResponseCode + "errorMsg::" + errorMsg);
		// }
		// };
		//
		// LoginBusiness loginBusiness = new LoginBusiness("sss", "123",
		// notify);
		// loginBusiness.doBusiness();
		//
		// RegisterBusiness registerBusiness = new RegisterBusiness("kim",
		// "123456", notify);
		// registerBusiness.doBusiness();
	}

	@Override
	protected void initView() {
		setContentView(R.layout.indexpage_activity);
		imageSwitcher = (ImageSwitcher) findViewById(R.id.imgsw_face);
		imageSwitcher.setFactory(this);
		imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
				R.anim.transition_in));
		imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
				R.anim.transition_out));
		startSwitchPictures();
		handler.postDelayed(new Runnable() {

			@Override
			public void run() {
				ActivityDispatch.toMain(IndexPageActivity.this);
				IndexPageActivity.this.finish();
			}
		}, ValueConstant.INDEX_PAGE_PRESENT_TIME);
	}

	private void startSwitchPictures() {

		if (!needRunning) {
			return;
		}

		if (drawables != null) { // 显示新下载的图片
			currentPIndex = currentPIndex % drawables.length;
			imageSwitcher.setImageDrawable(drawables[currentPIndex]);
		} else { // 显示默认图片
			currentPIndex = currentPIndex % DEFAULT_IMAGE_ARRAY.length;
			imageSwitcher.setImageResource(DEFAULT_IMAGE_ARRAY[currentPIndex]);
		}
		currentPIndex++;
		handler.sendEmptyMessageDelayed(MSG_SWITCH_P,
				ValueConstant.FACE_IMAGE_SHOW_INTERVAL);
	}

	@SuppressWarnings("deprecation")
	@Override
	public View makeView() {
		ImageView i = new ImageView(this);
		i.setBackgroundColor(0xFF000000);
		i.setScaleType(ImageView.ScaleType.FIT_XY);// 设置全屏
		i.setLayoutParams(new ImageSwitcher.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		return i;
	}

}
